package com.example.demo.manager.publisher;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.example.demo.manager.inf.CanalManager;
import com.example.demo.utils.CanalPropertyUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Description
 * @Author <a href="hedong.wu@baozun.com">wuhedong</a>
 * @Date 2020/5/26 18:03
 **/
@Slf4j
@ConditionalOnProperty(prefix = "canal", name = "type", havingValue = "sync",matchIfMissing = true)
@Component
@AllArgsConstructor
public class SyncCanalChangePublisherImpl<T> implements CanalChangePublisher<T> {

    private final Map<String, CanalManager<T>> canalManagerMap;

    @Override
    public void doEntrys(CanalEntry.RowChange rowChage, CanalManager<T> canalManager, CanalEntry.EventType eventType) {
        for (CanalEntry.RowData rowData : rowChage.getRowDatasList()) {
            if (handle(eventType, canalManager, rowData)) {
                CanalPropertyUtils.printColumn(rowData.getBeforeColumnsList());
                CanalPropertyUtils.printColumn(rowData.getAfterColumnsList());
            }
        }
    }

    @Override
    public CanalManager<T> getCanalManager(String tableName) {
        return canalManagerMap.get(tableName);
    }
}
